using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using SharpCore.Data;
using SharpCore.Extensions;
using SharpCore.Utilities;

namespace MMS.DAO
{
	public class OrderFormDAO
	{
		#region Fields

		private string connectionStringName;

		#endregion

		#region Constructors

		public OrderFormDAO(string connectionStringName)
		{
			ValidationUtility.ValidateArgument("connectionStringName", connectionStringName);

			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the OrderForm table.
		/// </summary>
		public void Insert(OrderFormDTO orderForm)
		{
			ValidationUtility.ValidateArgument("orderForm", orderForm);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@idrepository", orderForm.Idrepository),
				new SqlParameter("@idcustomer", orderForm.Idcustomer),
				new SqlParameter("@rentday", orderForm.Rentday),
				new SqlParameter("@vat", orderForm.Vat),
				new SqlParameter("@discount", orderForm.Discount),
				new SqlParameter("@building", orderForm.Building)
			};

			orderForm.Id = (int) SqlClientUtility.ExecuteScalar(connectionStringName, CommandType.StoredProcedure, "OrderFormInsert", parameters);
		}

		/// <summary>
		/// Updates a record in the OrderForm table.
		/// </summary>
		public void Update(OrderFormDTO orderForm)
		{
			ValidationUtility.ValidateArgument("orderForm", orderForm);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@id", orderForm.Id),
				new SqlParameter("@idrepository", orderForm.Idrepository),
				new SqlParameter("@idcustomer", orderForm.Idcustomer),
				new SqlParameter("@rentday", orderForm.Rentday),
				new SqlParameter("@vat", orderForm.Vat),
				new SqlParameter("@discount", orderForm.Discount),
				new SqlParameter("@building", orderForm.Building)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "OrderFormUpdate", parameters);
		}

		/// <summary>
		/// Deletes a record from the OrderForm table by its primary key.
		/// </summary>
		public void Delete(int id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@id", id)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "OrderFormDelete", parameters);
		}

		/// <summary>
		/// Deletes a record from the OrderForm table by a foreign key.
		/// </summary>
		public void DeleteAllByIdcustomer(int idcustomer)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@idcustomer", idcustomer)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "sp_OrderFormDeleteAllByIdcustomer", parameters);
		}

		/// <summary>
		/// Selects a single record from the OrderForm table.
		/// </summary>
		public OrderFormDTO Select(int id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@id", id)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "OrderFormSelect", parameters))
			{
				if (dataReader.Read())
				{
					return MapDataReader(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects a single record from the OrderForm table.
		/// </summary>
		public string SelectJson(int id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@id", id)
			};

			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "OrderFormSelect", parameters);
		}

		/// <summary>
		/// Selects all records from the OrderForm table.
		/// </summary>
		public List<OrderFormDTO> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "OrderFormSelectAll"))
			{
				List<OrderFormDTO> orderFormDTOList = new List<OrderFormDTO>();
				while (dataReader.Read())
				{
					OrderFormDTO orderFormDTO = MapDataReader(dataReader);
					orderFormDTOList.Add(orderFormDTO);
				}

				return orderFormDTOList;
			}
		}

		/// <summary>
		/// Selects all records from the OrderForm table.
		/// </summary>
		public string SelectAllJson()
		{
			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "OrderFormSelectAll");
		}

		/// <summary>
		/// Selects all records from the OrderForm table by a foreign key.
		/// </summary>
		public List<OrderFormDTO> SelectAllByIdcustomer(int idcustomer)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@idcustomer", idcustomer)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "sp_OrderFormSelectAllByIdcustomer", parameters))
			{
				List<OrderFormDTO> orderFormDTOList = new List<OrderFormDTO>();
				while (dataReader.Read())
				{
					OrderFormDTO orderFormDTO = MapDataReader(dataReader);
					orderFormDTOList.Add(orderFormDTO);
				}

				return orderFormDTOList;
			}
		}

		/// <summary>
		/// Selects all records from the OrderForm table by a foreign key.
		/// </summary>
		public string SelectAllByIdcustomerJson(int idcustomer)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@idcustomer", idcustomer)
			};

			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "sp_OrderFormSelectAllByIdcustomer", parameters);
		}

		/// <summary>
		/// Creates a new instance of the OrderFormDTO class and populates it with data from the specified SqlDataReader.
		/// </summary>
		private OrderFormDTO MapDataReader(SqlDataReader dataReader)
		{
			OrderFormDTO orderFormDTO = new OrderFormDTO();
			orderFormDTO.Id = dataReader.GetInt32("id", 0);
			orderFormDTO.Idrepository = dataReader.GetInt32("idrepository", 0);
			orderFormDTO.Idcustomer = dataReader.GetInt32("idcustomer", 0);
			orderFormDTO.Rentday = dataReader.GetDateTime("rentday", new DateTime(0));
			orderFormDTO.Vat = dataReader.GetInt32("vat", 0);
			orderFormDTO.Discount = dataReader.GetInt32("discount", 0);
			orderFormDTO.Building = dataReader.GetString("building", null);

			return orderFormDTO;
		}

		#endregion
	}
}
